package org.itworks.biodev.gui.panels;

import java.awt.event.ActionEvent;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;

import org.itworks.biodev.gui.*;
import org.itworks.biodev.model.*;
import org.itworks.helpers.mvc.*;

import com.toedter.calendar.JDateChooser;

public class OrdersPanelControler extends AbstractControler {
	
	private OrdersPanelModel model = null;
	
	public OrdersPanelControler(OrdersPanelModel model) {
		this.model = model;
	}

	public void viewLoaded() {
		try {
			this.model.setOrdersList(OrderTable.getInstance().findAll());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public OrdersPanelModel getModel() {
		return this.model;
	}

	public void createNewOrderButtonActionPerformed(ActionEvent actionEvent) throws Exception {
		// TODO Auto-generated method stub
		
		EditOrderDialogView editDialog = new EditOrderDialogView( new EditOrderDialogControler( new EditOrderDialogModel(), null));
		editDialog.setModal(true);
		editDialog.setVisible(true);
			
		try{
			this.model.setOrdersList(OrderTable.getInstance().findAll());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void buttonColumnOrdersActionPerformed(ActionEvent actionEvent,
			int selectedIndex) {
		// TODO Auto-generated method stub
		try {
			Order orderToEdit = this.model.getOrders().elementAt(selectedIndex);
			EditOrderDialogView editDialog = new EditOrderDialogView( new EditOrderDialogControler( new EditOrderDialogModel(),orderToEdit));
			editDialog.setModal(true);
			editDialog.setVisible(true);
			
			//if(editDialog.getIsOK()) {
				this.model.setOrdersList(OrderTable.getInstance().findAll());
			//}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void productListTableValueChanged(ListSelectionEvent listSelectionEvent, Order selectedOrder) {
		// TODO Auto-generated method stub
		this.model.setProductsList(selectedOrder);
	}

	public void deleteSelectedOrdersButtonActionPerformed(ActionEvent actionEvent, Vector<Integer> selectedIndexes) {
		if(selectedIndexes.size() > 0) {
			if(JOptionPane.showConfirmDialog(
					null,
					"Etes vous sur de vouloir supprimer les commandes selectionnnees ?",
					"Confirmation", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
				// Suppression
				for(Integer i : selectedIndexes) {
					Order o;
					try {
						o = OrderTable.getInstance().find(i);

						OrderTable.getInstance().delete(o);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				try {
					this.model.setOrdersList(OrderTable.getInstance().findAll());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else 
		{
			JOptionPane.showMessageDialog(null, "Aucune commande a annule n'a ete selectionne !", "Erreur", JOptionPane.ERROR_MESSAGE);
		}
		
	}	
	
	public void filterButtonActionPerformed(ActionEvent actionEvent, JDateChooser dateChooser, String orderId) throws NumberFormatException, Exception {
	boolean validDate=true;
	boolean validId=true;
	
	if(orderId.isEmpty()) {
		validId=false;
	}
	if(dateChooser.getDate()==null)
	{
		validDate=false;
	}
		try { 
				Integer.parseInt(orderId); 
				try {
					DateFormat dateFormat = new SimpleDateFormat(dateChooser.getDateFormatString());
					dateFormat.parse(dateChooser.getDate().toLocaleString());
				}
				catch (Exception e) {
					//e.printStackTrace();
					validDate = false;
				}
			} 
			catch (Exception e) { 
				//e.printStackTrace();
				validId = false;
			}	
		
		if (validDate==false && validId == false) {
			// Si les deux champs de filtrage ne sont pas valides, on ne faire rien
			this.model.setOrdersList(OrderTable.getInstance().findAll());
		}
		else if (validDate==false)
		{
			this.model.setOrdersList(OrderTable.getInstance().findByDateAndId(Integer.parseInt(orderId),new Date(),validDate, validId));	
		}
		else if (validId==false)
		{
			this.model.setOrdersList(OrderTable.getInstance().findByDateAndId(new Integer(0),dateChooser.getDate(),validDate, validId));	
		}
		else { // Quand tout est rempli
			this.model.setOrdersList(OrderTable.getInstance().findByDateAndId(Integer.parseInt(orderId),dateChooser.getDate(),validDate, validId));				
		}
	}

	public boolean isValidDate(String date)
	{
		  SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd");
		  
		  java.util.Date testDate = null;
	
		  try
		  {
		   testDate = sdf.parse(date);
		  }
		  catch (ParseException e)
		  {
		   return false; // Formation invalide
		  }
	
	
		  if (!sdf.format(testDate).equals(date)) 
		  {
		   return false;// Formation invalide
		  }
	  
		  return true;

	}
}
